package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.InventoryOrder;
import cn.stylefeng.guns.modular.system.mapper.InventoryOrderMapper;
import cn.stylefeng.guns.modular.system.model.params.InventoryOrderParam;
import cn.stylefeng.guns.modular.system.model.result.InventoryOrderResult;
import  cn.stylefeng.guns.modular.system.service.InventoryOrderService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author 小飞
 * @since 2019-06-28
 */
@Service
public class InventoryOrderServiceImpl extends ServiceImpl<InventoryOrderMapper, InventoryOrder> implements InventoryOrderService {


    @Autowired
    private InventoryOrderMapper inventoryOrderMapper;


    @Override
    public void add(InventoryOrderParam param){
        InventoryOrder entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(InventoryOrderParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(InventoryOrderParam param){
        InventoryOrder oldEntity = getOldEntity(param);
        InventoryOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public InventoryOrderResult findBySpec(InventoryOrderParam param){
        return null;
    }

    @Override
    public List<InventoryOrderResult> findListBySpec(InventoryOrderParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(InventoryOrderParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(InventoryOrderParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private InventoryOrder getOldEntity(InventoryOrderParam param) {
        return this.getById(getKey(param));
    }

    private InventoryOrder getEntity(InventoryOrderParam param) {
        InventoryOrder entity = new InventoryOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }



    /**
     * 根据商品的序列号进行查询
     */
    @Override
    public InventoryOrderResult getInventoryOrderId(@Param("serialNum") String serialNum){
        return inventoryOrderMapper.getInventoryOrderId(serialNum);
    }









}
